//
//  WJLastViewController.m
//  WJBSApp
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 王景伟. All rights reserved.
//
//  这个页面的网址  user-id是上个页面传入 + 关注的页面
//
// 头像: http://api.budejie.com/api/api_open.php?&a=profile&c=user&userid=9779590
// 帖子: http://s.budejie.com/topic/user-topic/9779590/1/desc/bs0315-iphone-4.2/0-20.json?
// 分享:http://s.budejie.com/topic/share-topic/9779590/bs0315-iphone-4.2/0-20.json?
// 评论:http://s.budejie.com/comment/user-comment/9779590/bs0315-iphone-4.2/0-20.json?
// 本页面是关注的最终界面

#import "WJLastViewController.h"
#import "WJMainTableViewCell.h"
#import "WJLastModel.h"
#import "WJMainModel.h"
#import "WJEssAndNewPLVC.h"
#import "WJDataManager.h"
#import "WJ_TJDY_VC.h"

@interface WJLastViewController ()<WJMainTableViewDelegate>

@property (nonatomic, strong) WJMainTableView *tableView;

@property (nonatomic,strong) WJBaseView *touView;
// 上面的图片
@property (nonatomic,strong) WJBaseImageView *backgroundImageView;
// 头像
@property (nonatomic,strong) WJBaseImageView *headerImageView;
// 性别
@property (nonatomic,strong) WJBaseImageView *sexImageView;

@property (nonatomic,strong) WJBaseLable *nameLable;

@property (nonatomic,strong) WJBaseLable *djLable;

@property (nonatomic,strong) WJBaseLable *jifenLable;

@property (nonatomic,strong) WJBaseLable *gzLable;

@property (nonatomic,strong) WJBaseLable *fsLable;

@property (nonatomic,strong) WJBaseLable *leftLable;

@property (nonatomic,strong) UIVisualEffectView *visualView;
// 存放下面的数据
@property (nonatomic,strong) NSMutableArray *array;
// 存放header数据
@property (nonatomic,strong) WJLastModel *model;

@property (nonatomic,retain) NSNumber *np;

@end

@implementation WJLastViewController

static NSString *const LastCell = @"LastCell";

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[WJVideoPlayView sharedPlayView] resetAVPlayer];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createView];
    
    [self createNavigationView];
    
    [self getHeaderData];
}

- (void)createNavigationView {
    self.array = [NSMutableArray array];

    self.touView = [[WJBaseView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.view addSubview:self.touView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, WIDTH - 200, 64)];
    lable.text = self.userName;
    lable.textAlignment = 1;
    lable.textColor = BSRedColor;
    [self.touView addSubview:lable];
    
    WJBaseButton *button = [WJBaseButton buttonWithTitle:nil frame:CGRectMake(0, 0, 45, 64) target:self action:@selector(tjLeftAction)];
    button.backgroundColor = BSClearColor;
    [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateHighlighted];
    [self.touView addSubview:button];
    
    //#warning +关注 需要判断啊  在数据库里面  要是有就显示 取消关注
    WJBaseButton *belowButton = [WJBaseButton buttonWithType:UIButtonTypeCustom dayBgColor:BSWhiteColor dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    belowButton.frame = CGRectMake(0, HEIGHT - 49, WIDTH, 49);
    belowButton.titleLabel.font = [UIFont systemFontOfSize:20];
    
    if ([[WJDataManager shareFMDB] isExistUserID:self.userID]) { //也就是存在
        [belowButton setTitle:@"取消关注" forState:0];
        [belowButton setTitleColor:BSGrayColor forState:0];
    } else {
        [belowButton setTitleColor:BSRedColor forState:0];
        [belowButton setTitle:@"+关注" forState:0];
    }
    
    [belowButton addTarget:self action:@selector(addFollow:) forControlEvents:1<<6];
    [self.view addSubview:belowButton];
}

- (void)createView {
    self.tableView = [WJMainTableView mainTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) controller:self];
    self.tableView.contentInset = UIEdgeInsetsMake(HEIGHT *2/5 + 110 , 0, 0, 0);
    self.tableView.scrollDelegate = self;
    self.tableView.headerEnabled = NO;
    [self.view addSubview:self.tableView];

    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getLeftNewData];
    }];
    header.ignoredScrollViewContentInsetTop = HEIGHT *2/5 + 110;
    self.tableView.mj_header = header;
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self loadMoreLeftData];
    }];
    
    self.backgroundImageView = [[WJBaseImageView alloc]initWithFrame:CGRectMake(0, - HEIGHT *2/5 - 110, WIDTH , HEIGHT *2/5)];
    self.backgroundImageView.backgroundColor = BSRedColor;
    [self.tableView addSubview:self.backgroundImageView];
    
#pragma mark - 毛玻璃效应
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualView =  [[UIVisualEffectView alloc]initWithEffect:effect];
    visualView.frame = self.backgroundImageView.frame;
    [self.tableView addSubview:visualView];
    self.visualView = visualView;
    
#pragma mark - 点击头像 可以 水波纹
    self.headerImageView = [[WJBaseImageView alloc]initWithFrame:CGRectMake(10, - 110 - WIDTH / 10 - 5, WIDTH / 5+10, WIDTH / 5 + 10)];
    self.headerImageView.layer.cornerRadius = WIDTH / 10 + 5;
    self.headerImageView.layer.masksToBounds = YES;
    self.headerImageView.backgroundColor = BSOrangeColor;
    [self.tableView addSubview:self.headerImageView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    self.headerImageView.userInteractionEnabled = YES;
    [self.headerImageView addGestureRecognizer:tap];
    
    self.sexImageView = [[WJBaseImageView alloc]initWithFrame:CGRectMake(30 +  WIDTH / 5, - 110 - 30, 25, 25)];
    self.sexImageView.layer.cornerRadius = 25/2;
    self.sexImageView.layer.masksToBounds = YES;
    [self.tableView addSubview:self.sexImageView];
    
    self.nameLable = [[WJBaseLable alloc]initWithFrame:CGRectMake(55 +  WIDTH / 5, - 110 - 30, WIDTH - 55 -  WIDTH / 5, 25) dayBgColor:BSWhiteColor nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    self.nameLable.backgroundColor = BSClearColor;
    self.nameLable.textColor = BSWhiteColor;
    [self.tableView addSubview:self.nameLable];
    
    self.djLable = [[WJBaseLable alloc]initWithFrame:CGRectMake(30 +  WIDTH / 5, -100, 100, 20) dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    [self.tableView addSubview:self.djLable];
    self.djLable.backgroundColor = BSClearColor;
    
    self.jifenLable = [[WJBaseLable alloc]initWithFrame:CGRectMake(30 +  WIDTH / 5, -70, 100, 20) dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    [self.tableView addSubview:self.jifenLable];
    self.jifenLable.backgroundColor = BSClearColor;
    
    self.gzLable = [[WJBaseLable alloc]initWithFrame:CGRectMake(WIDTH - 120, -100, 50, 20) dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    [self.tableView addSubview:self.gzLable];
    self.gzLable.backgroundColor = BSClearColor;
    self.gzLable.textAlignment = 1;
    
    WJBaseLable *lable1 = [[WJBaseLable alloc]initWithFrame:CGRectMake(WIDTH - 120, -70, 50, 20) dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    lable1.text = @"关注";
    lable1.textAlignment = 1;
    lable1.backgroundColor = BSClearColor;
    [self.tableView addSubview:lable1];
    
    self.fsLable = [[WJBaseLable alloc]initWithFrame:CGRectMake(WIDTH - 70, -100, 50, 20) dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    [self.tableView addSubview:self.fsLable];
    self.fsLable.backgroundColor = BSClearColor;
    self.fsLable.textAlignment = 1;
    
    WJBaseLable *lable = [[WJBaseLable alloc]initWithFrame:CGRectMake(WIDTH - 70, -70, 50, 20) dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    lable.text = @"粉丝";
    lable.backgroundColor = BSClearColor;
    lable.textAlignment = 1;
    [self.tableView addSubview:lable];
    
#pragma mark - button 的默认点击效果
    
    self.leftLable = [[WJBaseLable alloc]initWithFrame:CGRectMake(0, -40, WIDTH, 40)];
    [self.tableView addSubview:self.leftLable];
    self.leftLable.textAlignment = 1 ;
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)mainTableViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.tableView]) {
        CGFloat y = self.tableView.contentOffset.y + HEIGHT * 2 / 5 + 110;
        self.touView.backgroundColor = [BSWhiteColor colorWithAlphaComponent:y /200];
        if (y < 0) {
            self.backgroundImageView.frame = CGRectMake(y/2,y- HEIGHT * 2 / 5 - 110, WIDTH-y, -y+ HEIGHT * 2 / 5);
            self.visualView.alpha = -y / 200;
            self.visualView.frame = self.backgroundImageView.frame;
        }else{
            
            self.backgroundImageView.frame =CGRectMake(0,y/4- HEIGHT *2/5 - 110, WIDTH, HEIGHT * 2 / 5);
        }        
    }
}




- (void)getHeaderData {
    NSString *headerUrl = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?&a=profile&c=user&userid=%@",self.userID];
    [WJNetTool GET:headerUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {        
        NSDictionary *dataDic = resuposeObject[@"data"];
        self.model = [[WJLastModel alloc]initWithDic:dataDic];
        [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:self.model.background_image]];
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:self.model.profile_image]];
        if ([self.model.sex isEqualToString:@"m"]) {
            self.sexImageView.image = [UIImage imageNamed:@"Profile_manIcon"];
            self.sexImageView.backgroundColor = [UIColor blueColor];
        }else{
            self.sexImageView.image = [UIImage imageNamed:@"Profile_womanIcon"];
            self.sexImageView.backgroundColor = BSRedColor;
        }
        self.djLable.text = [NSString stringWithFormat:@"等级:%ld",(long)self.model.level];
        self.jifenLable.text = [NSString stringWithFormat:@"积分:%ld",(long)self.model.credit];
        self.gzLable.text = self.model.follow_count;
        self.fsLable.text = self.model.fans_count;
        self.nameLable.text = self.model.username;
        self.leftLable.text = [NSString stringWithFormat:@"我的帖子:%ld",(long)self.model.tiezi_count];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
    }];
}


- (void)getLeftNewData {
    NSString *leftUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/user-topic/%@/1/desc/bs0315-iphone-4.2/0-20.json?",self.userID];
    [WJNetTool GET:leftUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        if (dic[@"info"][@"np"] != nil) {
            self.np = dic[@"info"][@"np"];
        }
        [self.array removeAllObjects];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.array addObject:layout];
        }
        self.tableView.dataArr = self.array;
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreLeftData {
    if ([self.np isKindOfClass:[NSNull class]] || self.np == nil) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        return;
    }
    
    NSString *leftUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/user-topic/%@/1/desc/bs0315-iphone-4.2/%@-20.json?",self.userID,self.np];
    
    [WJNetTool GET:leftUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        self.np = dic[@"info"][@"np"];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.array addObject:layout];
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:@"加载失败"];
    }];
}


#pragma mark - 所有按钮的点击方法
///> 水波纹
- (void)tap:(UITapGestureRecognizer *)tap {
    CATransition *transition = [CATransition animation];
    transition.type = @"rippleEffect";    // 设置动画的类型
    transition.duration = 1;    // 设置动画时长
    transition.repeatCount = 2;    // 设置重复次数
    [self.headerImageView.layer addAnimation:transition forKey:@"transition"];
}

- (void)tjLeftAction {
    [self.navigationController popViewControllerAnimated:YES];
}
///> +关注
- (void)addFollow:(WJBaseButton *)button {
    NSString *string = button.titleLabel.text;
    if ([string isEqualToString:@"取消关注"]) {
        [button setTitle:@"+关注" forState:0];
        [button setTitleColor:BSRedColor forState:0];
        [[WJDataManager shareFMDB] deleteWith:self.userID];
    }else{
        [button setTitle:@"取消关注" forState:0];
        [button setTitleColor:BSGrayColor forState:0];
        [[WJDataManager shareFMDB] insertWithID:self.model.id username:self.model.username tiezi_count:self.model.tiezi_count follow_count:self.model.follow_count profile_image:self.model.profile_image];
    }
}

@end
